package cn.edu.nuc.onlinestore.vo;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import cn.edu.nuc.onlinestore.model.Goods;

public class Cart implements Serializable {
	
	private static final long serialVersionUID = -5523647580332725112L;
	
	
	private Map<String, Goods> cart = new HashMap<>();
	//购物车商品总价格
	private double totalPrice = 0;
	//购物车商品总价格
	private int totalQuantity = 0;
	private String msg;
	
	public String getMsg() {
		return msg;
	}
	public Cart(){}
	public Cart(double totalPrice,int totalQuantity){
		this.totalPrice=totalPrice;
		this.totalQuantity=totalQuantity;
	}
	/**
	 * 添加商品到购物车
	 * @param goods
	 * @param quantity
	 * @return 返回该商品在购物车内总数量
	 */
	public boolean add(String goodsname, int quantity){
		goodsDo goodsdo=new goodsDo();
		Map<String, Goods> map=goodsdo.showGoodList();
		Goods good;
		if(map.get(goodsname).getNum()<=quantity){
			if( cart.get( goodsname ) == null ){
				//如果maps 里没有该商品,直接添加
				good=map.get(goodsname);
				good.setNum(quantity);
				cart.put(goodsname,good);
				msg="增加了"+good.getNum()+"件"+goodsname;
				return true;
			} else {
				//如果maps里已存在当前添加商品,则累加商品数量
				cart.get(goodsname).setNum(quantity+cart.get(goodsname).getNum());
				msg="增加了"+cart.get(goodsname).getNum()+"件"+goodsname;
				return true;
			}
		}else{
			msg="sorry,商品不足，仅剩"+map.get(goodsname).getNum()+"件";
			return false;
		}
	}
	
	/**
	 * 将某件商品从购物车内移除
	 * @param goods 商品对象
	 * @return 
	 * @return 返回移除数量
	 */
	public  boolean remove(String goodname){
		
		int p=cart.get(goodname).getNum();
		cart.remove(goodname);
		//移除商品后调用计算购物车总金额、总价格方法
		msg="移除了商品"+goodname+p+"件";
		return true;
	}
	
	/**
	 * 根据商品id将商品从购物车内移除
	 * @param gid
	 * @return
	 */
	public boolean remove(int gid){
		Goods goods = null;
		int p=0;
		//遍历map，进行删除
		for(String goodname:getnamelist()){
			goods=cart.get(goodname);
			while(goods.getGid()==gid){
				p=goods.getNum();
				this.remove(goodname);
			}
		}
		msg="移除了商品编号为"+gid+"商品共"+p+"件";
		return true;
	}
	/**
	 * 获取此集合中的key列表，返回set集合
	 */
	public Set<String> getnamelist(){
		Set<String> keylist=cart.keySet();
		return keylist;
	}
	
	/**
	 * 计算购物车总金额、总数量方法
	 * @return 
	 */
	public boolean total(){
		//先将总金额、总数量归零
		this.totalPrice = 0;
		this.totalQuantity = 0;
		Goods good=null;
		String[] a=new String[getnamelist().size()];
		int b=0;
		for(String goodname:getnamelist()){
			good=cart.get(goodname);
			goodsDo goodsdo=new goodsDo();
			int num=(goodsdo.showGoodList().get(goodname).getNum()-good.getNum());
			if(num>=0){
				goodsdo.goodsUpdate(num, goodname);
				this.totalPrice=this.totalPrice+good.getPrice()*good.getNum();
				this.totalQuantity=this.totalQuantity+good.getNum();
				msg=goodname+"购买成功，共花费"+this.totalPrice;
			}else{
				msg=goodname+"数量不足,购买失败";
			}
			a[b]=msg;
			b++;
		}
		msg=a.toString();
		return true;
	}

	/**
	 * 返回购物车商品总金额(此处不应提供set方法,因为修改购物车商品金额不能由外面修改)
	 * @return
	 */
	public double getTotalPrice() {
		return totalPrice;
	}

	/**
	 * 返回购物车商品总数量(此处不应提供set方法,因为修改购物车商品数量不能由外面修改)
	 * @return
	 */
	public int getTotalQuantity() {
		return totalQuantity;
	}
	/**
	 * 遍历map,返回一个set对象数组(存储已购买的所有商品)，用于显示用户已购买的列表
	 * @return
	 */
	public int gettotalnum(){
		int totalnum=0;
		for(String goodname:getnamelist()){
			Goods good=cart.get(goodname);
			totalnum=totalnum+good.getNum();
			}
		return totalnum;
	}
	public Map<String,Goods> getGoodsshow(){
		msg="返回列表如下";
		return cart; 
	}
}
